home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryByReference / TiledImage.java.z / TiledImage.java
Encoding:
Java Source  |  2003-08-08  |  8.7 KB  |  352 lines

  1. /*
  2.  *    @(#)TiledImage.java 1.9 02/04/01 15:04:05
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.image.*;
  42. import java.util.Vector;
  43. import java.awt.color.ColorSpace;
  44.  
  45. public class TiledImage extends Object implements RenderedImage {
  46.     
  47.  
  48.     WritableRaster[][] tile = new WritableRaster[3][3];
  49.     
  50.     WritableRaster bigTile;
  51.     ComponentColorModel colorModel;
  52.     BufferedImage checkBoard;
  53.     int minX = -2;
  54.     int minY = -1;
  55.  
  56.     TiledImage() {
  57.     ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); 
  58.     int[] nBits = {8, 8, 8, 8};
  59.     int i, j, k, cc = 255;
  60.     int[] bandOffset = new int[4];
  61.     colorModel =
  62.         new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
  63.     // Create 9 tiles
  64.     bandOffset[0] = 3;
  65.     bandOffset[1] = 2;
  66.     bandOffset[2] = 1;
  67.     bandOffset[3] = 0;
  68.     for (i = 0; i < 3; i++) {
  69.         for (j = 0; j < 3; j++) {
  70.         tile[i][j] = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 8, 8 , 32, 4, bandOffset, null);
  71.         }
  72.     }
  73.  
  74.     // tile {-2, -1}
  75.     byte[] byteData = ((DataBufferByte)tile[0][0].getDataBuffer()).getData();
  76.     for (i=4, k = 8 * 4 * 4+4 * 4;i < 8;i++, k+= 16){
  77.         for (j=4;j < 8;j++, k+=4){
  78.         byteData[k] = (byte)0;
  79.         byteData[k+1] = (byte)cc; 
  80.         byteData[k+2] = (byte)0;
  81.         byteData[k+3] = (byte)cc ;
  82.         }
  83.     }
  84.  
  85.     // tile {-1, -1}
  86.     byteData = ((DataBufferByte)tile[1][0].getDataBuffer()).getData();
  87.     for (i=4, k = 8 * 4 * 4;i < 8;i++){
  88.         for (j=4;j < 8;j++, k+=4){
  89.         byteData[k] = (byte)0;
  90.         byteData[k+1] = (byte)cc; 
  91.         byteData[k+2] = (byte)0;
  92.         byteData[k+3] = (byte)cc ;
  93.         }
  94.         for (j=4;j < 8;j++, k+=4){
  95.         byteData[k] = (byte)0;
  96.         byteData[k+1] = (byte)0; 
  97.         byteData[k+2] = (byte)0;
  98.         byteData[k+3] = (byte)cc ;
  99.         }
  100.     }
  101.  
  102.     // tile {1, -1}
  103.     byteData = ((DataBufferByte)tile[2][0].getDataBuffer()).getData();
  104.     for (i=4, k = 8 * 4 * 4;i < 8;i++, k+= 16){
  105.         for (j=0;j < 4;j++, k+=4){
  106.         byteData[k] = (byte)0;
  107.         byteData[k+1] = (byte)0; 
  108.         byteData[k+2] = (byte)0;
  109.         byteData[k+3] = (byte)cc ;
  110.         }
  111.     }
  112.  
  113.     // tile {-2, 0}
  114.     byteData = ((DataBufferByte)tile[0][1].getDataBuffer()).getData();
  115.     for (i=0, k = 16;i < 4;i++, k+=16){
  116.         for (j=4;j < 8;j++, k+=4){
  117.         byteData[k] = (byte)0;
  118.         byteData[k+1] = (byte)cc; 
  119.         byteData[k+2] = (byte)0;
  120.         byteData[k+3] = (byte)cc ;
  121.         }
  122.     }
  123.     for (i=4, k = 8*4*4+16;i < 8;i++, k+=16){
  124.         for (j=4;j < 8;j++, k+=4){
  125.         byteData[k] = (byte)0;
  126.         byteData[k+1] = (byte)0; 
  127.         byteData[k+2] = (byte)cc;
  128.         byteData[k+3] = (byte)0 ;
  129.         }
  130.     }
  131.     // tile {-1, 0}
  132.     byteData = ((DataBufferByte)tile[1][1].getDataBuffer()).getData();
  133.     for (i=0, k = 0;i < 4;i++){
  134.         for (j=4;j < 8;j++, k+=4){
  135.         byteData[k] = (byte)0;
  136.         byteData[k+1] = (byte)cc; 
  137.         byteData[k+2] = (byte)0;
  138.         byteData[k+3] = (byte)cc ;
  139.         }
  140.         for (j=4;j < 8;j++, k+=4){
  141.         byteData[k] = (byte)0;
  142.         byteData[k+1] = (byte)0; 
  143.         byteData[k+2] = (byte)0;
  144.         byteData[k+3] = (byte)cc ;
  145.         }
  146.     }
  147.     for (i=0, k = 8 * 4 * 4;i < 4;i++){
  148.         for (j=4;j < 8;j++, k+=4){
  149.         byteData[k] = (byte)0;
  150.         byteData[k+1] = (byte)0; 
  151.         byteData[k+2] = (byte)cc;
  152.         byteData[k+3] = (byte)0 ;
  153.         }
  154.  
  155.         for (j=4;j < 8;j++, k+=4){
  156.         byteData[k] = (byte)0;
  157.         byteData[k+1] = (byte)0; 
  158.         byteData[k+2] = (byte)cc;
  159.         byteData[k+3] = (byte)cc ;
  160.         }        
  161.  
  162.     }
  163.  
  164.  
  165.     // tile {0, 0}
  166.     byteData = ((DataBufferByte)tile[2][1].getDataBuffer()).getData();
  167.     for (i=0, k = 0;i < 4;i++, k+= 16) {
  168.         for (j=4;j < 8;j++, k+=4){
  169.         byteData[k] = (byte)0;
  170.         byteData[k+1] = (byte)0; 
  171.         byteData[k+2] = (byte)0;
  172.         byteData[k+3] = (byte)cc ;
  173.         }
  174.     }
  175.     for (i=4, k = 8 * 4* 4;i < 8;i++, k+= 16) {
  176.         for (j=4;j < 8;j++, k+=4){
  177.         byteData[k] = (byte)0;
  178.         byteData[k+1] = (byte)0; 
  179.         byteData[k+2] = (byte)cc;
  180.         byteData[k+3] = (byte)cc ;
  181.         }
  182.     }
  183.     
  184.         
  185.     // tile {-2, 1}
  186.     byteData = ((DataBufferByte)tile[0][2].getDataBuffer()).getData();
  187.     for (i=4, k = 16;i < 8;i++, k+= 16) {
  188.         for (j=4;j < 8;j++, k+=4){
  189.         byteData[k] = (byte)0;
  190.         byteData[k+1] = (byte)0; 
  191.         byteData[k+2] = (byte)cc;
  192.         byteData[k+3] = (byte)0 ;
  193.         }
  194.     }
  195.  
  196.  
  197.     // tile {-1, 1}
  198.     byteData = ((DataBufferByte)tile[1][2].getDataBuffer()).getData();
  199.     for (i=0, k = 0;i < 8;i++) {
  200.         for (j=4;j < 8;j++, k+=4){
  201.         byteData[k] = (byte)0;
  202.         byteData[k+1] = (byte)0; 
  203.         byteData[k+2] = (byte)cc;
  204.         byteData[k+3] = (byte)0 ;
  205.         }
  206.         for (j=4;j < 8;j++, k+=4){
  207.         byteData[k] = (byte)0;
  208.         byteData[k+1] = (byte)0; 
  209.         byteData[k+2] = (byte)cc;
  210.         byteData[k+3] = (byte)cc ;
  211.         }
  212.     }
  213.  
  214.  
  215.  
  216.     // tile {0, 1}
  217.     byteData = ((DataBufferByte)tile[2][2].getDataBuffer()).getData();
  218.     for (i=4, k = 0;i < 8;i++, k+= 16) {
  219.         for (j=4;j < 8;j++, k+=4){
  220.         byteData[k] = (byte)0;
  221.         byteData[k+1] = (byte)0; 
  222.         byteData[k+2] = (byte)cc;
  223.         byteData[k+3] = (byte)cc ;
  224.         }
  225.     }
  226.     
  227.     bigTile =  Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 16, 16 , 64, 4, bandOffset, null);;
  228.         byteData = ((DataBufferByte)bigTile.getDataBuffer()).getData(); 
  229.         for (i=0, k = 0;i < 8;i++){
  230.             for (j=0;j < 8;j++, k+=4){
  231.         byteData[k] = (byte)0;
  232.                 byteData[k+1] = (byte)cc; 
  233.                 byteData[k+2] = (byte)0;
  234.                 byteData[k+3] = (byte)cc ;
  235.             } 
  236.             for (;j < 16;j++, k+=4){ 
  237.         byteData[k] = (byte)0;
  238.                 byteData[k+1] = (byte)0; 
  239.                 byteData[k+2] = (byte)0;
  240.                 byteData[k+3] = (byte)cc ;
  241.             } 
  242.         } 
  243.         for (;i < 16;i++){
  244.             for (j=0;j < 8;j++, k+=4){ 
  245.         byteData[k] = (byte)0;
  246.                 byteData[k+1] = (byte)0; 
  247.                 byteData[k+2] = (byte)cc;
  248.                 byteData[k+3] = (byte)0;
  249.             } 
  250.             for (;j < 16;j++, k+=4){ 
  251.         byteData[k] = (byte)0;
  252.                 byteData[k+1] = (byte)0; 
  253.                 byteData[k+2] = (byte)cc;
  254.                 byteData[k+3] = (byte)cc ;
  255.             } 
  256.         }
  257.     checkBoard = new BufferedImage(colorModel, bigTile, false, null);
  258.     }
  259.  
  260.  
  261.     
  262.     // create  four tiles {r, g, b, y}
  263.     public WritableRaster copyData(WritableRaster raster) {
  264.     return checkBoard.copyData(raster);
  265.     }
  266.  
  267.     public ColorModel getColorModel() {
  268.     return checkBoard.getColorModel();
  269.     }
  270.  
  271.     public Raster getData() {
  272.     return checkBoard.getData();
  273.     }
  274.  
  275.     public Raster getData(Rectangle rect) {
  276.     return checkBoard.getData(rect);
  277.     }
  278.  
  279.     public int getHeight() {
  280.     return 16;
  281.     }
  282.  
  283.     public int getMinTileX() {
  284.     return minX;
  285.     }
  286.  
  287.     public int getMinTileY() {
  288.     return minY;
  289.     }
  290.  
  291.     public int getMinX () {
  292.     return -8;
  293.     }
  294.  
  295.     public int getMinY () {
  296.     return -8;
  297.     }
  298.  
  299.     public int getNumXTiles() {
  300.     return 3;
  301.     }
  302.  
  303.     public int getNumYTiles() {
  304.     return 3;
  305.     }
  306.  
  307.     public Object getProperty(String name) {
  308.     return checkBoard.getProperty(name);
  309.     }
  310.  
  311.     public String[] getPropertyNames() {
  312.     return checkBoard.getPropertyNames();
  313.     }
  314.  
  315.  
  316.     public SampleModel getSampleModel() {
  317.     return checkBoard.getSampleModel();
  318.     }
  319.  
  320.     public Vector getSources() {
  321.     return null;
  322.     }
  323.  
  324.     public Raster getTile(int tileX, int tileY) {
  325.     return tile[tileX- minX][tileY - minY];
  326.     }
  327.  
  328.     public int getTileGridXOffset() {
  329.     return 4;
  330.     }
  331.  
  332.     public int getTileGridYOffset() {
  333.     return -4;
  334.     }
  335.  
  336.  
  337.     public int getTileHeight() {
  338.     return 8;
  339.     }
  340.  
  341.  
  342.     public int getTileWidth() {
  343.     return 8;
  344.     }
  345.  
  346.     public int getWidth() {
  347.     return 16;
  348.     }
  349. }
  350.     
  351.  
  352.